#!/bin/bash

# 自定义进度条更新的函数，包括额外信息
update_progress() {
    local progress=$1
    local total=$2
    local elapsed_time=$3
    local start_time=$4
    local bar_length=50

    # 计算进度条长度
    local filled_length=$((progress * bar_length / total))

    # 创建进度条
    printf "\rProgress: [%-${bar_length}s] %d%%" $(printf "%0.s=" $(seq 1 $filled_length)) $(printf "%0.s " $(seq 1 $((bar_length - filled_length)))) $((progress * 100 / total))

    # 计算剩余时间和预计完成时间
    local elapsed=$((elapsed_time * 1000))
    local total_time=$((total * 1000))
    local remaining_time=$((total_time - elapsed))
    localeta=$((remaining_time / 1000))

    # 显示额外信息
    printf " - Step: %d/%d - ETA: %ss" $progress $total $eta
}

# 记录开始时间
start_time=$(date +%s%N)

# 模拟任务进度
for i in $(seq 1 100); do
    echo "more command with it"
    elapsed_time=$(( $(date +%s%N) - start_time ))
    update_progress $i 100 $elapsed_time $start_time
    echo "you could also add"
    sleep 0.1
done

echo
echo "Task completed."

